package com.tjp.eight.ui.fragment.whozan;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.google.gson.Gson;
import com.tjp.eight.R;
import com.tjp.eight.ui.fragment.BaseFragment;
import com.tjp.eight.utils.IsSuccessUtils;
import com.tjp.eight.utils.ToatUtils;
import com.tjp.eight.views.FindItemDecoration;
import com.tjp.eight.views.rcview.HeaderViewRecyclerAdapter;

/**
 * Created by Administrator on 2017/3/2.
 */
public class FragmentWhoZan extends BaseFragment<WhoZanPresenter> implements WhoZanView {
    private String coid;
    private int page = 0;
    private RecyclerView rv_who_zan;
    private WhoZanAdapter whoZanAdapter;
    private HeaderViewRecyclerAdapter headerViewRecyclerAdapter;
    private View footView;

    @Override
    protected WhoZanPresenter createPresenter() {
        return new WhoZanPresenter(this);
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        presenter.loadData(getPostJson());
    }

    private String getPostJson() {
        WhoZanPostModel postModel = new WhoZanPostModel();
        postModel.coid = coid;
        postModel.page = page;
        return new Gson().toJson(postModel);
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        contentView = super.onCreateView(inflater, container, savedInstanceState);
        if (contentView == null) {
            contentView = inflater.inflate(R.layout.fragment_who_zan, container, false);
        }
        coid = getActivity().getIntent().getStringExtra("coid");
        initViews();
        return contentView;
    }


    @Override
    public void loadSuccess(WhoZanResultModel model) {
        if (model == null) return;
        if (IsSuccessUtils.isSuccess(model.result)) {
            whoZanAdapter.addData(model);
            if (model.data == null || model.data.isEmpty() || model.data.size() < 15) {
                headerViewRecyclerAdapter.removeFootByIndex(0);
            } else {
                if (headerViewRecyclerAdapter.getFooterCount() <= 0) {
                    headerViewRecyclerAdapter.addFooterView(footView);
                }
            }
        }
    }

    @Override
    public void loadError(String msg) {
        ToatUtils.toastCommentFail(getContext(), msg);
    }

    private void initViews() {
        rv_who_zan = (RecyclerView) contentView.findViewById(R.id.rv_who_zan);
        rv_who_zan.setLayoutManager(new LinearLayoutManager(getContext(), LinearLayoutManager.VERTICAL, false));
        rv_who_zan.addItemDecoration(new FindItemDecoration());
        whoZanAdapter = new WhoZanAdapter(getActivity());
        headerViewRecyclerAdapter = new HeaderViewRecyclerAdapter(whoZanAdapter);
        footView = LayoutInflater.from(getContext()).inflate(R.layout.swipe_refresh_footer, rv_who_zan, false);
        headerViewRecyclerAdapter.addFooterView(footView);
        rv_who_zan.setAdapter(headerViewRecyclerAdapter);
    }
}
